﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace carsitem.Commodity
{
    public partial class CommodityMaintenanceModifyForm : Form
    {
        public int GoodsNumber;
        //皮肤组件声明
        Sunisoft.IrisSkin.SkinEngine se = null;
        string SkinName = "SteelBlack";

        public CommodityMaintenanceModifyForm()
        {
            InitializeComponent();

            //皮肤组件引用
            se = new Sunisoft.IrisSkin.SkinEngine();
            se.SkinAllForm = true;
            se.SkinFile = "skin/" + SkinName + ".ssk";
        }
        private void CommodityMaintenanceModifyForm_Load(object sender, EventArgs e)
        {

        }

        //修改按钮
        private void Modify_Click(object sender, EventArgs e)
        {
            try//捕捉全空文本框异常
            {
                //sql修改语句
                string sql = string.Format("update Commodity set CarName = '{0}', Carcolor = '{1}', TreasuryPrices = '{2}', OutboundPrice = '{3}', Introducecar = '{4}'where GoodsNumber = '{5}'", this.textCarName.Text.Trim(), this.textCarcolor.Text.Trim(), this.textTreasuryPrices.Text.Trim(), this.textOutboundPrice.Text.Trim(), this.textIntroducecar.Text.Trim(), this.textGoodsNumber.Text.Trim());
                //判断是否成功
                if (DBHelper.Instance().ExcuteSql(sql))
                {
                    MessageBox.Show("修改成功！");

                    //将数据传输给父窗口
                    CommodityMaintenanceForm ower = (CommodityMaintenanceForm)this.Owner;
                    ower.LoadCangKu();
                    
                    //连接父窗口
                    carsitem.Commodity.CommodityMaintenanceForm commodityMaintenanceForm = new carsitem.Commodity.CommodityMaintenanceForm();//把form1传给form2
                    //弹出父窗口
                    commodityMaintenanceForm.Show();
                    //关闭自己
                    this.Close();
                }
                else
                {
                    MessageBox.Show("修改失败！");
                }

                //清除文本框数据
                this.textCarName.Clear();
                this.textCarcolor.Clear();
                this.textTreasuryPrices.Clear();
                this.textOutboundPrice.Clear();
                this.textIntroducecar.Clear();
                this.textGoodsNumber.Clear();
            }
            catch (Exception)
            {
                MessageBox.Show("请输入要修改的数据！");//文本框中无数据弹出
            }
        }

        //数字验证
        public bool IsNumberic(string oText)
        {
            string pattern = @"^\d+(\.\d)?$";
            if (oText != "")
            {
                if (!Regex.IsMatch(oText, pattern))
                    MessageBox.Show("请输入数字！");
            }
            return true;
        }

        //汉字验证
        public bool IsChineseCharacters(string oText)
        {
            string text = @"[\u4E00-\u9FA5]+$";
            if (oText != "")
            {
                if (!Regex.IsMatch(oText.ToString(), text))
                    MessageBox.Show("请输入汉字！");
            }
            return true;
        }

        //非空验证
        public bool Null(string oText)
        {
            if (oText == "")
            {
                MessageBox.Show("必须填写！");
            }
            return true;
        }

        //表单验证
        private void textGoodsNumber_Leave(object sender, EventArgs e)//商品编号
        {
            string oText = textGoodsNumber.Text.Trim();
            IsNumberic(oText);
            Null(oText);
        }

        private void textCarTypeID_Leave(object sender, EventArgs e)//汽车编号
        {
            string oText = textCarTypeID.Text.Trim();
            IsNumberic(oText);
            Null(oText);
        }

        private void textCarName_Leave(object sender, EventArgs e)//商品名称
        {
            string oText = textCarName.Text.Trim();
            IsChineseCharacters(oText);
            Null(oText);
        }

        private void textCarcolor_Leave(object sender, EventArgs e)//颜色
        {
            string oText = textCarcolor.Text.Trim();
            IsChineseCharacters(oText);
            Null(oText);
        }

        private void textTreasuryPrices_Leave(object sender, EventArgs e)//进货价格
        {
            string oText = textTreasuryPrices.Text.Trim();
            IsNumberic(oText);
            Null(oText);
        }

        private void textOutboundPrice_Leave(object sender, EventArgs e)//销售价格
        {
            string oText = textOutboundPrice.Text.Trim();
            IsNumberic(oText);
            Null(oText);
        }
    }
}
